<?php
/**
 * Model for News Component
 * @subpackage  news component
 * @license GNU/GPL
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
require_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'models'.DS.'layouttemplates.php');

/**
 * News Model
 *
 * @package    Joomla
 * @subpackage Components
 */
class JanewsModelfrontpage extends JanewsModellayouttemplates
{
	/**
	 * @return total templates
	 */
	function getTotal()
	{
		$db = JFactory::getDBO();
		$query = 'SELECT count(*) FROM #__janews_frontpage';
		$db->setQuery($query);
		$num = $db->loadResult();
		return $num;
	}
	
	/**
	 * @return List layout templates order by type from database
	 */
	function getLists($filter_order='', $filter_order_Dir='', $limit='', $limitstart='')
	{
	  $db = JFactory::getDBO();
		$query = 'SELECT s.id, s.title, s.headline as headline, s.blog as blog, s.default_f as published, s.sections FROM  #__janews_frontpage AS s'
			.' ORDER BY '.$filter_order.' '. $filter_order_Dir .', s.id';
		if ($limit=='' && $limitstart==''){
			$db->setQuery( $query);
		}else{
			$db->setQuery( $query, $limitstart, $limit);
		}
		$list = $db->loadObjectList();
		if(count($list)<1) {
		  //update janews_templates table
		  //$this->updateTemplates();
		  //get id of headline and blog
		  $query2 = "SELECT id FROM #__janews_templates
                 GROUP BY type
                 ORDER BY type";
		  $db->setQuery( $query2 );
		  $id_tmpl = $db->loadResultArray();
		  //get ids of published sections
		  $id_sec = $this->getStrListSecIds();
		  //insert new row into frontpage table
		  $query3 = "INSERT INTO `#__janews_frontpage` (`id`, `title`, `headline`, `blog`, `sections`, `default_f`)
		              VALUES (1, 'frontpage', '".$id_tmpl[1]."', '".$id_tmpl[0]."', '".$id_sec."', '1')";
		  $db->setQuery( $query3 );
			$db->query();
		  if ($limit=='' && $limitstart==''){
				$db->setQuery( $query);
			}else{
				$db->setQuery( $query, $limitstart, $limit);
			}
			$list = $db->loadObjectList();
		}
		return $list;
	}

	/**
	 * @return published categories of the section
	 */
	function getListSecs()
	{
		$db =& JFactory::getDBO();
		$query = 'SELECT id AS value, concat(concat(title,"\ ("), concat(id,"\) ") ) AS text FROM #__sections '.
						 'WHERE published = 1 ORDER BY ordering';
		$db->setQuery( $query );
		$list = $db->loadObjectList();
		return $list;
	}
	
	/**
	 * @param ID of the frontpage
	 * @return selected categories to display of the frontpage layout 
	 */
	function getSelectedSecs($id)
	{
		if ($id) {
			$str = $this->getStrSecIds($id);
			$db = JFactory::getDBO();
			$query = 'SELECT id AS value, concat(concat(title,"\ ("), concat(id,"\) ") ) AS text '.
			'FROM #__sections WHERE id IN ('."$str".')';
			$list = $this->_getList($query);
			$sort = JArrayHelper::toObject(split(',',$str));
			
			foreach ($sort as $i => $id) {
			  foreach ($list as $j => $v) {
			    if ($v->value==$id) {
			      $tmp = new stdClass();
			      $tmp = $list[$j];
			      $list[$j] = $list[$i];
			      $list[$i] = $tmp;
			    }
			  }
			}
		}
		return $list;
	}
	
	/**
	 * @param ID of the section
	 * @return string selected cat_id of the section 
	 */
	function getStrSecIds($id)
	{
		if ($id) {
			$db = JFactory::getDBO();
			$query = 'SELECT sections FROM #__janews_frontpage WHERE id ='.$id;
			$db->setQuery($query);
			$list = $db->loadResult();
		}
		return $list;
	}
	
	/**
	 * @return string list sec_ids of the section 
	 */
	function getStrListSecIds()
	{
		$db = JFactory::getDBO();
		$query = 'SELECT id FROM #__sections '.
						 'WHERE published = 1 ORDER BY ordering';
		$db->setQuery($query);
		$str = implode(',', $db->loadResultArray());

		return $str;
	}
	
	/**
	 * @return Info of frontpage
	 */
	function getFrontpageInfo($id)
	{
		$db = JFactory::getDBO();
		$query = 'SELECT * FROM #__janews_frontpage'.
		' WHERE id = '.$id;
		$db->setQuery($query);
		$rs = $db->loadObject();
		return $rs;
	}
	
	/**
	 * Check default home page
	 */
	function checkDefault($id)
	{
	  $db = JFactory::getDBO();
	  $query = "SELECT default_f"
	   ." FROM #__janews_frontpage"
	   ." WHERE id = ".$id;
	  $db->setQuery($query);
	  $check = $db->loadResult();
	  return $check;	   
	}
	/**
	 * update default the frontpage layout
	 *
	 * @param int $cid
	 */
	function updateDefault($cid)
	{
	  $db = JFactory::getDBO();
	  $query = 'UPDATE #__janews_frontpage '.
		'SET default_f = 0 WHERE default_f = 1';
		$db->setQuery($query);
		$db->loadObject();
		
		$query = 'UPDATE #__janews_frontpage '.
		'SET default_f = 1 WHERE id = '.$cid;
		$db->setQuery($query);
		$db->loadObject();
	}
	
}

?>